/**
 * respons js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */

var  consts  = require('../consts');
var  product=require('../db/product');
var cache=require('../redis/usercache');


function processCatalogList(request,response,callback){
		
	var data =request.data;
	var param = null;
   var cataIndex=16384;
   var muuid = null;
   var search = null;
   var sellStatus=-3;
   var online =0;
   if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return false;
	}else{
		 muuid=request.muuid;
	}
   
   if(request.online ===1){
	    online=1;
  }
   
	if(data!==null && typeof(data)!=='undefined'){
		console.log(data);
		
		try {
		    param = JSON.parse(data);

		} catch (e) {
			
		}


		if(param===null){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
		}
		
		if(param.cataIndex!==null && typeof(param.cataIndex)!=='undefined'){	
			cataIndex=param.cataIndex;
		}
		
       if(param.search!==null && typeof(param.search)!=='undefined'){	
			search=param.search;
		}
       if(param.sellStatus===1 || param.sellStatus ===0 || param.sellStatus===-1 || param.sellStatus===-2){
    	   sellStatus = param.sellStatus;
       }
		
	}
	
	product.listCatalog(muuid,cataIndex,search,sellStatus, online, null,function(wrap){
		if(wrap===null){

			response.errorCode=-1;
			response.message='暂无数据';
			if(callback){
				callback(response);
				return false;
			}
		}else{
			
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
			response.data = JSON.stringify(wrap);

			response.token=request.token;
			if(callback){
				callback(response);
			}
			return true;
			
		}
		
	});

	
}




function processProductList(request,response,callback){
	
	var data =request.data;
	
	var param = null;
	var cataID=-1;
    var muuid = null;
    var reserve=-1;
    var search =null;
    var sellStatus=-3;
	var deskNO=null;

    if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return false;
	}else{
		muuid=request.muuid;
	}
    
	if(data!==null && typeof(data)!=='undefined'){
		console.log(data);
		
		try {
		    param = JSON.parse(data);

		} catch (e) {
			
		}

		if(param===null){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
		}
		
		
		
		if(param.cataID!==null && typeof(param.cataID)!=='undefined'){	
			cataID=param.cataID;
		}
		
		if(param.reserve!==null && typeof(param.reserve)!=='undefined'){	
			reserve=param.reserve;
		}
		
		if(param.search!==null && typeof(param.search)!=='undefined'){	
			search=param.search;
		}
		if(param.sellStatus===0 || param.sellStatus ===-1 || param.sellStatus ===1 || param.sellStatus == -2){
			sellStatus=param.sellStatus;
		}
		if(param.deskNO){
			deskNO=param.deskNO;
		}
		
	}
	
	product.listProductByCataID(muuid,cataID,reserve,search,sellStatus,deskNO, function(wrap){
		if(wrap===null){

			response.errorCode=-1;
			response.message='暂无数据';
			if(callback){
				callback(response);
				return false;
			}
		}else{
			
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
			response.data = JSON.stringify(wrap);

			response.token=request.token;
			if(callback){
				callback(response);
			}
			return true;
			
		}
		
	});

	
	
}


function processSale(request,response,callback){
	console.log('detail1111');

	var data =request.data;
	var muuid='';
	var param = null;
	var IDs = null;
	var sellStatus=0;
	
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return false;
		
	}else{
		muuid=request.muuid;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.IDs){
		IDs= param.IDs;
	}
	
	if(param.sellStatus){
		sellStatus= param.sellStatus;
	}
	
	if(sellStatus !==0 && sellStatus !==1 && sellStatus !==-1){
		sellStatus=0;
	}
	
	if(!IDs){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}else {
	}
	
	 //addOrUpdate(callback)	
	product.sellStatusUpdate(IDs,muuid,sellStatus, function(obj){
		if(obj===null){
			response.errorCode=-1;
			response.message='暂无数据';
		}else{
			response.errorCode=consts.Error.NONE_ERROR;
			response.message=consts.Message.OK_MESSAGE;
		}
		if(callback){
			callback(response);
		}
	});
	
	return;

}





function processUpdate(request,response,callback){
	console.log('detail1111');

	var data =request.data;
	var withProfile=0;
	var muuid='';
	var param = null;
	var ID = null;
	var name=null;
	var desc=null
	var price = 0.0;
    var cataID=0;
    var temp = 1;
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return false;
		
	}else{
		muuid=request.muuid;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.name){
		name= param.name;
	}
	if(param.desc){
		desc= param.desc;
	}
	if(!desc){
		desc= name;
	}
	if(param.price){
		price= param.price;
	}
	
	if(param.cataID){
		cataID= param.cataID;
	}
	
	 //addOrUpdate(callback)	
	product.addOrUpdate(ID,muuid,name,desc, price,cataID,temp, function(obj){
		if(obj===null){
			response.errorCode=-1;
			response.message='暂无数据';
		}else{
			response.errorCode=consts.Error.NONE_ERROR;
			response.message=consts.Message.OK_MESSAGE;
			response.data=JSON.stringify(obj);
		}
		if(callback){
			callback(response);
		}
	});
	
	return;

}




function processDetail(request,response,callback){
	console.log('detail1111');

	var data =request.data;
	var withProfile=0;
	var muuid='';
	var param = null;
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return false;
		
	}else{
		muuid=request.muuid;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	if(param.ID===null || typeof(param.ID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.withProfile!==null && typeof(param.withProfile)!=='undefined'){
		withProfile = param.withProfile;
	}
	
		
	product.getDetail(param.ID, muuid, withProfile, function(obj){
		if(obj===null){
			response.errorCode=-1;
			response.message='暂无数据';
		}else{
			response.errorCode=consts.Error.NONE_ERROR;
			response.message=consts.Message.OK_MESSAGE;
			response.data=JSON.stringify(obj);
		}
		if(callback){
			callback(response);
		}
	});
	
	return;

}



function processSalesRankingReport(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;
	
	try {
		param=JSON.parse(data);
	} catch (e) {
	}
	var type = 1;
//	value = new Date().get
	if(param !==null && typeof(param.type) !== 'undefined' &&  param.type !== null )  {
		type = param.type;
	} 
	
	if(type !==1 && type !==7 && type!== 30 &&type !==12 && type !==4 ) {
		type =1;
	}
	var datetime = new Date();
	
	
	if(param !==null && typeof(param.datetime) !== 'undefined' &&  param.datetime !== null &&  param.datetime !== 0 )  {
		datetime.setTime(param.datetime);
	}
	
	
	var cataID =0;
	var ranking =1;
	var count =20;

	var storeID = -1;
	
	if(param !==null && typeof(param.cataID) !== 'undefined' &&  param.cataID !== null &&  param.cataID !== 0 )  {
		cataID =  param.cataID;
	}
	
	if(param !==null && typeof(param.storeID) !== 'undefined' &&  param.storeID !== null &&  param.storeID !== 0 )  {
		storeID =  param.storeID;
	}
	
	if(param !==null && typeof(param.ranking) !== 'undefined' &&  param.ranking !== null &&  param.ranking !== 0 )  {
		ranking =  param.ranking;
	}
	if(param !==null && typeof(param.count) !== 'undefined' &&  param.count !== null &&  param.count !== 0 )  {
		count =  param.count;
	}

	
	product.getSalesRankingReport(request.token,storeID, type,datetime,cataID,ranking,count, function(err,wrap){
		if(err){
			response.errorCode = err.number;
			response.message= consts.message;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}else {
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}
		
	});

	return;

}

function processSalesStatisticsReport(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;
	
	try {
		param=JSON.parse(data);
	} catch (e) {
	}
	var type = 1;
//	value = new Date().get
	if(param !==null && typeof(param.type) !== 'undefined' &&  param.type !== null )  {
		type = param.type;
	} 
	
	if(type !==1 && type !==7 && type!== 30 &&type !==12 && type !==4 ) {
		type =1;
	}
	var datetime = new Date();
	
	
	if(param !==null && typeof(param.datetime) !== 'undefined' &&  param.datetime !== null &&  param.datetime !== 0 )  {
		datetime.setTime(param.datetime);
	}
	
	var isFood = 0;

	var storeID = -1;
	
	if(param !==null && typeof(param.isFood) !== 'undefined' &&  param.isFood !== null &&  param.isFood !== 0 )  {
		isFood =  param.isFood;
	}
	
	if(param !==null && typeof(param.storeID) !== 'undefined' &&  param.storeID !== null &&  param.storeID !== 0 )  {
		storeID =  param.storeID;
	}
	
	
	product.getSalesStatisticsReport(request.token,storeID, type,datetime,isFood, function(err,wrap){
		if(err){
			response.errorCode = err.number;
			response.message= consts.message;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}else {
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			if(callback){
				callback(response);
			}
		}
		
	});

	return;

}


module.exports = {
		processDetail:processDetail,
		processUpdate:processUpdate,
		processSale:processSale,
		processSalesRankingReport:processSalesRankingReport,
		processProductList:processProductList,
		processCatalogList:processCatalogList,
		processSalesStatisticsReport:processSalesStatisticsReport,
		
	};

